package com.example.utils;

/**
 * @author chentao
 * @version 1.0
 * @description: TODO
 * @date 2025/3/22 下午6:28
 */
public class SerializableUtils {

    /***
     * 反序列化成对象
     * @param bytes
     * @return
     */
    public static Object deserialize(byte[] bytes, Class<?> clazz) throws Exception {
        return ObjectMapperUtils.getDefaultObjectMapper().readValue(bytes, clazz);
    }


    /***
     * 序列化对象
     * @param obj
     * @return
     * @throws Exception
     */
    public static byte[] serialize(Object obj) throws Exception {
        return ObjectMapperUtils.getDefaultObjectMapper().writeValueAsBytes(obj);
    }

    /***
     * 把对象序列化json形式
     * @param obj
     * @return
     * @throws Exception
     */
    public static String serializeToString(Object obj) throws Exception {
        return ObjectMapperUtils.getDefaultObjectMapper().writeValueAsString(obj);
    }

    /***
     * 把字符串序列化成对象
     * @param str
     * @param clazz
     * @return
     * @throws Exception
     */
    public static Object deserializeFromString(String str, Class<?> clazz) throws Exception {
        return ObjectMapperUtils.getDefaultObjectMapper().readValue(str, clazz);
    }
}
